package com.stylefeng.guns.modular.clm.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.clm.service.ITeacherRoomService;
import com.stylefeng.guns.modular.system.model.ClmRoom;
import com.stylefeng.guns.modular.system.model.Teacher;
import com.stylefeng.guns.modular.system.model.TeacherRoom;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-05-29 16:04:25
 */
@Controller
@RequestMapping("/teacherRoom")
public class TeacherRoomController extends BaseController {

    private String PREFIX = "/clm/teacherRoom/";

    @Autowired
    private ITeacherRoomService teacherRoomService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "teacherRoom.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/teacherRoom_add")
    public String teacherRoomAdd() {
        return PREFIX + "teacherRoom_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/teacherRoom_update/{teacherRoomId}")
    public String teacherRoomUpdate(@PathVariable Integer teacherRoomId, Model model) {
        TeacherRoom teacherRoom = teacherRoomService.selectById(teacherRoomId);
        model.addAttribute("item",teacherRoom);
        LogObjectHolder.me().set(teacherRoom);
        return PREFIX + "teacherRoom_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
    	String id=this.getPara("id");
    	String teacherId=this.getPara("teacherId");
    	String siteId=this.getPara("siteId");
        List<ClmRoom> l_clmRoom = teacherRoomService.selectTeacherRooms(id, teacherId, siteId);
        return l_clmRoom;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TeacherRoom teacherRoom) {
        teacherRoomService.insert(teacherRoom);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer id) {
        teacherRoomService.deleteById(id);
        return SUCCESS_TIP;
    }
    
    /**
     * 上下架
     */
    @RequestMapping(value = "/downOrUp")
    @ResponseBody
    public Object downOrUp(@RequestParam Integer id) {
    	TeacherRoom  teacherRoom=teacherRoomService.selectById(id);
    	if("0".equals(teacherRoom.getStatus())){
    		teacherRoom.setStatus("1");//状态 1-开启 0-禁用
    	}else{
    		teacherRoom.setStatus("0");//状态 1-开启 0-禁用
    	}
    	/*Date date_now=new Date();
    	teacherRoom.setUpdateTime(date_now);*/
    	teacherRoomService.updateById(teacherRoom);
        return SUCCESS_TIP;
    }
    
    /**
     * 置顶或取消置顶
     */
    @RequestMapping(value = "/topOrNo")
    @ResponseBody
    public Object topOrNo(@RequestParam Integer id) {
    	TeacherRoom  teacherRoom=teacherRoomService.selectById(id);
    	if("0".equals(teacherRoom.getBeTop())){
    		teacherRoom.setBeTop("1");//置顶 0-否 1-是
    	}else{
    		teacherRoom.setBeTop("0");//置顶 0-否 1-是
    	}
    	Date date_now=new Date();
    	teacherRoom.setUpdateTime(date_now);
    	teacherRoomService.updateById(teacherRoom);
        return SUCCESS_TIP;
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TeacherRoom teacherRoom) {
        teacherRoomService.updateById(teacherRoom);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{teacherRoomId}")
    @ResponseBody
    public Object detail(@PathVariable("teacherRoomId") Integer teacherRoomId) {
        return teacherRoomService.selectById(teacherRoomId);
    }
}
